package ace.module.stock.core.impl.dao.aggregate.helper;

import ace.cmp.util.AceStreamUtils;
import ace.module.stock.api.enums.WmsStockRecordActionTypeEnum;
import ace.module.stock.api.enums.WmsStockRecordReverseFlagEnum;
import ace.module.stock.core.impl.dao.entity.WmsStockRecord;
import java.util.List;
import java.util.stream.Collectors;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Component;

/**
 * 库存 aggregate.
 *
 * @author Caspar
 */
@AllArgsConstructor
@Component
public class WmsStockRecordAggregateHelper {

  public WmsStockRecord findById(List<WmsStockRecord> wmsStockRecords, Long id) {
    return AceStreamUtils.ofNullable(wmsStockRecords)
        .filter(p -> id.equals(p.getId()))
        .findFirst()
        .orElse(null);
  }

  public List<Long> getStockIdsFromList(List<WmsStockRecord> wmsStockRecords) {
    return AceStreamUtils.ofNullable(wmsStockRecords)
        .map(WmsStockRecord::getStockId)
        .collect(Collectors.toList());
  }

  public WmsStockRecord findByStockId(List<WmsStockRecord> wmsStockRecords, Long stockId) {
    return AceStreamUtils.ofNullable(wmsStockRecords)
        .filter(p -> stockId.equals(p.getStockId()))
        .findFirst()
        .orElse(null);
  }

  public boolean hasOnlyAddActionType(List<WmsStockRecord> wmsStockRecords) {

    return AceStreamUtils.ofNullable(wmsStockRecords)
        .noneMatch(p -> !WmsStockRecordActionTypeEnum.ADD.getCode().equals(p.getActionType()));
  }

  public boolean hasOnlySubtractActionType(List<WmsStockRecord> wmsStockRecords) {

    return AceStreamUtils.ofNullable(wmsStockRecords)
        .noneMatch(p -> !WmsStockRecordActionTypeEnum.SUBTRACT.getCode().equals(p.getActionType()));
  }

  public boolean hasOnlyAddOrSubtractActionType(List<WmsStockRecord> wmsStockRecords) {

    return AceStreamUtils.ofNullable(wmsStockRecords)
        .noneMatch(p ->
            !WmsStockRecordActionTypeEnum.ADD.getCode().equals(p.getActionType()) ||
                !WmsStockRecordActionTypeEnum.SUBTRACT.getCode().equals(p.getActionType())
        );
  }

  public boolean hasOnlyReverseFlagIsNone(List<WmsStockRecord> wmsStockRecords) {

    return AceStreamUtils.ofNullable(wmsStockRecords)
        .noneMatch(p -> WmsStockRecordReverseFlagEnum.NONE.getCode().equals(p.getReverseFlag()));
  }

  public boolean hasOnlySetActionType(List<WmsStockRecord> wmsStockRecords) {

    return AceStreamUtils.ofNullable(wmsStockRecords)
        .noneMatch(p -> !WmsStockRecordActionTypeEnum.SET.getCode().equals(p.getActionType()));
  }
}